package methodTest

import (
	"fmt"
	"math/rand"
	"sort"
)

type Hero struct {
	Name string
	Age  int
}
type HeroSlice []Hero

func (h HeroSlice) Len() int {
	return len(h)
}

// Less /*按heronode的年龄从大到小排序*/
func (hs HeroSlice) Less(i, j int) bool {
	return hs[i].Age < hs[j].Age
}

func (hs HeroSlice) Swap(i, j int) {
	//临时变量
	/*	temp:=hs[i]
		hs[i]=hs[j]
		hs[j]=temp*/

	//交换  等同于上面三句话
	hs[i], hs[j] = hs[j], hs[i]
}

type student struct {
	Name  string
	Age   int
	Score float64
}

func NodeTest() {
	var intSlice = []int{0, -1, 10, 7, 90}
	sort.Ints(intSlice)
	fmt.Println(intSlice)

	var heroes HeroSlice

	for i := 0; i < 10; i++ {
		hero := Hero{
			Name: fmt.Sprintf("英雄 | %d", rand.Intn(100)),
			Age:  rand.Intn(100),
		}

		heroes = append(heroes, hero)
	}
	fmt.Println("排序前\n")
	for k, _ := range heroes {
		fmt.Printf("name=%v,age=%v\n", heroes[k].Name, heroes[k].Age)
	}
	sort.Sort(heroes)
	fmt.Println("排序后\n")

	for k, _ := range heroes {
		fmt.Printf("name=%v,age=%v\n", heroes[k].Name, heroes[k].Age)
	}

}
